package com.kalix.ksh.proint.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.ILineChartRequestBeanService;
import com.kalix.ksh.proint.api.dao.ILineChartRequestBeanDao;
import com.kalix.ksh.proint.api.model.PostRequestUtil;
import com.kalix.ksh.proint.entities.HistogramRequestBean;
import com.kalix.ksh.proint.entities.HistogramTitleBean;
import com.kalix.ksh.proint.entities.LineChartRequestBean;
import com.kalix.ksh.proint.entities.LineChartTitleBean;
import org.json.JSONArray;
import org.json.JSONObject;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class LineChartRequestBeanServiceImpl extends GenericBizServiceImpl<ILineChartRequestBeanDao, LineChartRequestBean> implements ILineChartRequestBeanService {
    public LineChartRequestBeanServiceImpl(){
        super.init(LineChartRequestBean.class.getName());
    }


    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_POST","config.ksh.portconf");

    /**
     * @Description 获取折线图服务的数据
     * @auther: lwj
     * @date: 2018/10/29 0029 上午 10:07
     */
    public String getValue() {

        String sql = "select li from LineChartRequestBean li";
        List<LineChartRequestBean> list = dao.find(sql);
        List<LineChartTitleBean> l = getListTitle();
        long ms=System.currentTimeMillis();
        long cache_time = ms - 90000000;
        long start_absolute = ms - 3000000;
        JSONObject jObject = new JSONObject();
        String one ="";
        JSONArray names = new JSONArray();
        JSONArray values = new JSONArray();
        int  number = Integer.parseInt(l.get(0).getMax());
        for (int i = 0; i < number; i++) {
            JSONObject dataList = new JSONObject();
            String uuid = list.get(i).getUuid();
            values.put(list.get(i).getName());
            String strJson = "{" + "\"metrics\":[" + "{" + "\"tags\":{" +
                    "\"mo\":[" + "\"Line.domain=\\\"defaultEngine\\\",uuid=\\\""+uuid+"\\\"\"" +
                    "]" + "}," + "\"name\":\"ifOctets\"" + "}" + "]," +
                    "\"cache_time\":"+ cache_time +   "," + "\"start_absolute\":" + start_absolute + "}";
            String json = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/api/v1/datapoints/query",strJson);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray js = jsonObject.getJSONArray("queries");
            JSONObject jj = js.getJSONObject(0);
            JSONArray ss = jj.getJSONArray("results");
            JSONObject s = ss.getJSONObject(0);
            JSONArray arr = s.getJSONArray("values");
            int nu = 0;
            if(number>5){
                 nu = number;
            }else{
                 nu = 5 ;
            }
            String twoArr[] = new String[nu];
            if(arr.length()<5){
                    for (int o = 0; o < 5; o++) {
                        JSONArray jarr =  arr.getJSONArray(o);
//                        if(jarr.getLong(1)>0){
                            twoArr[o] = "0";

//                        }
                    }
                    ArrayList listt = new ArrayList();
                    String str = "{";
                    for(int k = 1; k <= number; k++){
                        listt.removeAll(listt);
                        for(int p = 0; p < k; p++) {
                            listt.add(twoArr[p]);
                        }
                        if (k == number) {
                            str += "'"+ (k-1) + "':" +listt;
                        } else {
                            str += "'" + (k-1) + "':" +listt + "," ;
                        }
                    }
                    str += "}";
                    int num = i+1;
                    jObject.put("dataList"+num, str);
                    names.put("线"+num);
            }else{

                for (int j = 0; j < 5; j++) {
                    JSONArray jarr =  arr.getJSONArray(j);
                    one = String.valueOf((jarr.getLong(1) / 1024));
                    twoArr[j] = one;
                }
                ArrayList listt = new ArrayList();
                String str = "{";
                for(int k = 1; k <= (number>5?number:5); k++){
                    listt.removeAll(listt);
                    for(int j = 0; j < k; j++) {
                        listt.add(twoArr[j]);
                    }
                    if (k == (number>5?number:5)) {
                        str += "'"+ (k-1) + "':" +listt;
                    } else {
                        str += "'" + (k-1) + "':" +listt + "," ;
                    }
                }
                str += "}";
                int num = i+1;
                jObject.put("dataList"+num, str);
                names.put("线"+num);
            }
        }
        jObject.put("pList",values);

        JSONArray xAxisData = new JSONArray();
        JSONArray titleData = new JSONArray();
        for (int i = 0; i < 5; i++){
            xAxisData.put(l.get(0).getTitle());
            titleData.put(l.get(0).getTitle());
        }
        jObject.put("xAxisData",xAxisData);
        JSONObject yAxis = new JSONObject();
        yAxis.put("name",l.get(0).getYaxisname());
        yAxis.put("max",l.get(0).getMax());
        jObject.put("yAxis",yAxis);
        jObject.put("lValue",names);
        jObject.put("xAxisName",l.get(0).getAxisname());
        jObject.put("titleData",titleData);
        String stri = jObject.toString();
        return stri;
    }
    /**
     * @Description 获取柱状图服务数据
     * @auther: lwj
     * @date: 2018/10/30 0030 上午 9:34
     */
    public String getColumnValue() {
        String sql = "select li from HistogramRequestBean li";
        List<HistogramRequestBean> list = dao.find(sql);
        List<HistogramTitleBean> l = getHisTitle();
        long ms=System.currentTimeMillis();
        long cache_time = ms - 90000000;
        long start_absolute = ms - 3000000;
        JSONObject jObject = new JSONObject();
        JSONArray values = new JSONArray();
        JSONObject dataList = new JSONObject();
        int  number = Integer.parseInt(l.get(0).getMax());
         for (int i = 0; i < number; i++) {
            String uuid = list.get(i).getUuid();
//            System.out.println(list.get(i));
            values.put(list.get(i).getName());
            String strJson = "{" + "\"metrics\":[" + "{" + "\"tags\":{" +
                    "\"mo\":[" + "\"Line.domain=\\\"defaultEngine\\\",uuid=\\\""+uuid+"\\\"\"" +
                    "]" + "}," + "\"name\":\"ifOctets\"" + "}" + "]," +
                    "\"cache_time\":"+ cache_time +   "," + "\"start_absolute\":" + start_absolute + "}";
            String json = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/api/v1/datapoints/query",strJson);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray js = jsonObject.getJSONArray("queries");
            JSONObject jj = js.getJSONObject(0);
            JSONArray ss = jj.getJSONArray("results");
            JSONObject s = ss.getJSONObject(0);
            JSONArray arr = s.getJSONArray("values");
            String one ="";
            if(arr.length() >= number) {
                JSONArray jar = new JSONArray();
                for (int j = 0; j < number; j++) {
                    JSONArray jarr = arr.getJSONArray(j);
                    one = String.valueOf((jarr.getLong(1) / 1024));
                    jar.put(one);
                }
                String st = "" + i;
                dataList.put(st, jar);
                jObject.put("dataList", dataList);

            }else{
                JSONArray jar = new JSONArray();
                for (int j = 0; j < number; j++) {
                    one = "0" ;
                    jar.put(one);
                }
                String st = "" + i;
                dataList.put(st, jar);
                jObject.put("dataList", dataList);

            }
        }
        jObject.put("pList",values);
        JSONArray xAxisData = new JSONArray();
        JSONArray titleData = new JSONArray();

        for (int i = 0; i < number; i++){
            xAxisData.put(i);
            titleData.put(l.get(0).getTitle());
        }
        jObject.put("xAxisData",xAxisData);
        JSONObject yAxis = new JSONObject();
        yAxis.put("name",l.get(0).getYaxisname());
        yAxis.put("max",5);
        jObject.put("yAxis",yAxis);
        jObject.put("xAxisName",l.get(0).getAxisname());
        jObject.put("titleData",titleData);
        String stri = jObject.toString();
        return stri;
    }


    /**
     * @Description 截取double类型数据小数点后两位小数
     * @auther: lwj
     * @date: 2018/10/29 0029 上午 11:32
     */
    public String  getString(double dou){
        DecimalFormat df   = new DecimalFormat("##########0.00");
        return df.format(dou);
    }
/**
 *
 * 功能描述: 获取柱状图标题
 * @User wanghw
 * @Date 下午 3:03 2018/11/5 0005
 * @Param 
 * @return 
 */
    public List<HistogramTitleBean> getHisTitle(){
        String sql = "select ht from HistogramTitleBean ht";
        List<HistogramTitleBean> list = dao.find(sql);
        return list;
    }
/**
 *
 * 功能描述: 获取折线图标题
 * @User wanghw
 * @Date 下午 3:03 2018/11/5 0005
 * @Param 
 * @return 
 */
    public List<LineChartTitleBean> getListTitle(){
        String sql = "select lt from LineChartTitleBean lt";
        List<LineChartTitleBean> list = dao.find(sql);
        return list;
    }
}
